import json
from xtools.utils import write_json_lines, read_json_lines

df = read_json_lines('d:/cluener_public/train.json')
test = read_json_lines('d:/cluener_public/dev.json')


def convert(data, out_name):
    out = []
    for line in data:
        label = line.pop('label')
        line['entities'] = []
        for k, v in label.items():
            for _, spans in v.items():
                for start, end in spans:
                    line['entities'].append((k, start, end))
        out.append(line)

    write_json_lines(out, out_name)


convert(df, 'train.json')
convert(test, 'dev.json')
